@echo OFF
 
REM Before running this script, please ensure that the SSL certificate is installed on the machine
REM This batch file creates a virtual directory and a physical directory in the location specified
REM It sets up the virtual directory with Browse and Execute permissions
REM Sets up the virtual directory with Basic Authentication and SSL
 
if '%1' == '/?' goto Usage
if '%1' == '?' goto Usage
if '%1' == '-?' goto Usage
if '%1' == '' goto Usage
 
:parm_loop_top
 
   REM See if any more were processed 
   if '%1'=='' goto parmsdone
 
   set paramname=%1
   set paramvalue=%2
 
   if /I '%paramname%'=='-vdir'           set vdir=%paramvalue%
   if /I '%paramname%'=='-vdir'           goto parmok
 
   if /I '%paramname%'=='-vdirfolder'     set vdirfolder=%paramvalue%
   if /I '%paramname%'=='-vdirfolder'     goto parmok
 
   if /I '%paramname%'=='-scriptfilename' set scriptfilename=%paramvalue%
   if /I '%paramname%'=='-scriptfilename' goto parmok

   if /I '%paramname%'=='-realmname' 	  set realmname=%paramvalue%
   if /I '%paramname%'=='-realmname'      goto parmok
  
 
   echo ERROR: Unhandled parameter specified: %1
   goto Usage
 
   :parmok
 
   shift
   shift
   
   goto parm_loop_top
 
:parmsdone
 
if '%vdir%' == '' goto Usage
if '%vdirfolder%' == '' goto Usage
if '%scriptfilename%' == '' goto Usage
if '%realmname%' == '' goto Usage
 
REM set vdir=%1
REM set vdirfolder=%systemdrive%\%2
REM set scriptfilename=%3
REM set realmname=%4
 
REM Display Parameters
echo.
echo -----------------------------------
echo Settings:
echo -vdir:       %vdir%
echo -vdirfolder  %vdirfolder%
echo -scriptfilename    %scriptfilename%
echo -realmname    %realmname%
echo -----------------------------------
echo.
 
if exist "%ProgramFiles%\Microsoft SQL Server\100\COM\replisapi.dll"  set agentpathtemp="%ProgramFiles%\Microsoft SQL Server\100\COM"
 
md "%vdirfolder%"
if errorlevel 1 goto error
 
copy /Y %agentpathtemp:~0,-1%\replisapi.dll" "%vdirfolder%"
if errorlevel 1 goto error
 
cscript %scriptfilename% create_vdir "W3SVC/1/Root/%vdir%"
if errorlevel 1 goto error
cscript %scriptfilename% appcreateoutproc "W3SVC/1/Root/%vdir%"
if errorlevel 1 goto error
 
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AppFriendlyName" "%vdir%"
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/Path" "%vdirfolder%"
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AuthAnonymous" false
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AuthBasic" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AuthNTLM" false
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/EnableDefaultDoc" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/DirBrowseShowDate" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/DirBrowseShowTime" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/DirBrowseShowSize" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/DirBrowseShowExtension" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/DirBrowseShowLongDate" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessRead" false
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessWrite" false
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessSource" false
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessExecute" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessScript" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessNoRemoteRead" false
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessNoRemoteWrite" false
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessNoRemoteExecute" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessNoRemoteScript" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/AccessSSLFlags" 8
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/EnableDirBrowsing" true
if errorlevel 1 goto error
cscript %scriptfilename% set "W3SVC/1/Root/%vdir%/Realm" %realmname%
if errorlevel 1 goto error

REM Enable a Web Service Extension for the replisapi.dll.
if exist %vdirfolder%\replisapi.dll (
cscript %windir%\system32\iisext.vbs /AddFile %vdirfolder%\replisapi.dll 1 WebSync 1 WebSync
cscript %windir%\system32\iisext.vbs /AddDep "SQL Server" WebSync
)
if errorlevel 1 goto error
 
cacls "%vdirfolder%" /E /T /G "NETWORK SERVICE":F
if errorlevel 1 goto error
goto end
 
:error
echo.
echo ------------------------------------------------------------
echo !!!!! An error occurred !!!!!
echo ------------------------------------------------------------
goto end
 
:Usage
echo -----------------------------------------------------------------
echo.
echo SYNTAX: %0 -vdir ^<virtual Directory Name^> -vdirfolder ^<Physical Folder Name and Path^> -scriptfilename ^<location of adsutil.vbs script^> -realmname ^<Name of the authentication realm^> 
echo Eg: public_makevdir.bat WEBSYNC C:\WEBSYC_VDIR C:\Inetpub\AdminScripts\adsutil.vbs
echo.
echo -----------------------------------------------------------------
 
:end
exit


